from django.shortcuts import render
from rest_framework.generics import GenericAPIView, ListAPIView
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.mixins import ListModelMixin
from rest_framework.filters import OrderingFilter

from booktest.models import BookInfo
from goods.models import SKU
from goods.serializers import SKUSerializer




# Create your views here.

# GET /categories/(?P<category_id>\d+)/skus/
class SKUListView(ListAPIView):
    serializer_class = SKUSerializer
    # queryset = SKU.objects.filter(category_id=category_id, is_launched=True)

    def get_queryset(self):
        """返回分类SKU商品数据的查询集"""
        category_id = self.kwargs["category_id"]
        return SKU.objects.filter(category_id=category_id, is_launched=True)

    # 排序设置
    filter_backends = [OrderingFilter]
    ordering_fields = ('update_time', 'price', 'sales')


        # def get(self, request, category_id):
    #     """
    #     获取分类sku商品的数据：
    #     1. 根据categorey_id获取分类下sku商品数据
    #     2. 将商品的数据序列化并返回
    #     """
    #
    #     # 1.根据categorey_id获取分类下sku商品数据
    #     skus = self.get_queryset()
    #
    #     # 2.将商品的数据序列化并返回
    #     serializer = self.get_serializer(skus, many=True)
    #     return Response(serializer.data)
